﻿using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using System;
using System.Collections;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Spring.Context.Support;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;

namespace Huirui.Cavan.Application.MainBoundedContext.SalesModule
{
	#region SalesOrder

    public class SalesOrderAppService : BaseAppService<SalesOrder, string, SalesOrderSearch, ISalesOrderRepository>, ISalesOrderAppService
	{
        private ICustomerAppService CustomerAppService { get; set; }
        private IWareHouseAppService WareHouseAppService { get; set; }
        private IStockAppService StockAppService { get; set; }

        public override BoolGenericResult<SalesOrder> AddOrUpdate(SalesOrder entity)
        {
            Init();
            BoolGenericResult<SalesOrder> result = new BoolGenericResult<SalesOrder>();

            this.CustomerAppService = this.SpringContext.GetObject(SystemModuleConst.CUSTOMERAPPSERVICE) as ICustomerAppService;
            this.WareHouseAppService = this.SpringContext.GetObject(StockModuleConst.WAREHOUSEAPPSERVICE) as IWareHouseAppService;
            this.StockAppService = this.SpringContext.GetObject(StockModuleConst.STOCKAPPSERVICE) as IStockAppService;
            Employee employee = this.EmployeeAppService.GetSingleBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id));
            Seller seller = new Seller(employee);

            SalesOrder oldSalesOrder = null;
            if (entity.Id.IsNotEmpty())
            {
                oldSalesOrder = this.Repository.Get(entity.Id);
            }
            if (entity.Customer.IsNotNull() && entity.Customer.Id.IsNotEmpty())
            {
                entity.Customer = this.CustomerAppService.GetById(entity.Customer.Id);
            }
            if (entity.SaleEmployee.IsNotNull() && entity.SaleEmployee.Id.IsNotEmpty())
            {
                entity.SaleEmployee = this.EmployeeAppService.GetById(entity.SaleEmployee.Id);
            }
            if (entity.WareHouse.IsNotNull() && entity.WareHouse.Id.IsNotEmpty())
            {
                entity.WareHouse = this.WareHouseAppService.GetById(entity.WareHouse.Id);
            }
            foreach (SalesOrderDetail sod in entity.SalesOrderDetails)
            {
                sod.WareHouse = entity.WareHouse;
                sod.Stock = this.StockAppService.GetById(sod.Stock.Id);
            }

            if (oldSalesOrder.IsNull())
            {
                IIDGenerateAppService idGenerateAppService = this.SpringContext.GetObject(SystemModuleConst.IDGENERATEAPPSERVICE, new object[] { employee.Corporation }) as IIDGenerateAppService;
                entity.Id = idGenerateAppService.GenerateID(SystemIdentifier.ReceiptCodeRuleType.SalesOrder);

                result = seller.AddSalesOrder(entity.Id, entity.SaleDate, entity.DeliveryDate, entity.Currency, entity.CurrencyRate, entity.CustomerOrderCode, entity.DeliveryAddress,
                    entity.CheckType, entity.Remark, entity.DiscountRate, entity.DiscountAmount, entity.ReceiptType, entity.ReceiptCondition, entity.PayDay, entity.DeadDay, entity.Customer,
                    entity.SaleEmployee, entity.WareHouse, entity.Project, entity.SalesOrderDetails);
                if (result.IsSuccess)
                {
                    result = base.Add(result.Object);
                }
            }
            else
            {
                result = seller.UpdateSalesOrder(oldSalesOrder, entity.SaleDate, entity.DeliveryDate, entity.Currency, entity.CurrencyRate, entity.CustomerOrderCode, entity.DeliveryAddress,
                    entity.CheckType, entity.Remark, entity.DiscountRate, entity.DiscountAmount, entity.ReceiptType, entity.ReceiptCondition, entity.PayDay, entity.DeadDay, entity.Customer,
                    entity.SaleEmployee, entity.WareHouse, entity.Project, entity.SalesOrderDetails);
                if (result.IsSuccess)
                {
                    result = base.Update(result.Object);
                }
                
            }

            result.Messages.Add("保存成功");

            return result;
        }
	}
	#endregion
}